package com.zhentao.controller;

import com.zhentao.Exception.vo.ResultVo;
import com.zhentao.dto.RoleDto;
import com.zhentao.pojo.SysRole;
import com.zhentao.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @RequestMapping("roleAdd")
    //角色的增加
    public ResultVo roleAdd(@RequestBody RoleDto roleDto){
        SysRole sysRole = new SysRole();
        sysRole.setRoleName(roleDto.getName());
        sysRole.setRoleCode(UUID.randomUUID().toString().replace("-", "").substring(0, 10));
        sysRole.setDescription(roleDto.getDescription());
        sysRole.setCreateTime(new Date());
        sysRole.setIsDeleted(1);
        //判断是否添加成功
        if(sysRoleService.save(sysRole)){
            return  ResultVo.success(sysRole);
        }else {
            return  ResultVo.error();
        }
    }
    @RequestMapping("roleUpdate")
    //角色的修改
    public ResultVo roleUpdate(@RequestBody RoleDto roleDto){
        SysRole sysRole = new SysRole();
        sysRole.setId(roleDto.getId());
        sysRole.setRoleName(roleDto.getName());
        sysRole.setDescription(roleDto.getDescription());
        sysRole.setUpdateTime(new Date());
        return sysRoleService.updateById(sysRole)?ResultVo.success():ResultVo.error();
    }
    @RequestMapping("roleDelete")
    //角色的删除
    public ResultVo roleDelete(Long id){
            return sysRoleService.removeById(id)?ResultVo.success():ResultVo.error();
    }
}
